package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.common.result.Result;
import com.system.runtai.entity.*;
import com.system.runtai.service.*;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 需求专区收藏 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "需求专区收藏")
@RestController
@RequestMapping("/runtai/need-collect")
public class NeedCollectController {

    @Autowired
    private NeedCollectService needCollectService;

    @Autowired
    private NeedService needService;

    @Autowired
    private NeedDiscussService needDiscussService;

    @Autowired
    private PostedCollectService postedCollectService;
    @Autowired
    private UpvoteService upvoteService;

    @ApiOperation(value = "程序的需求专区收藏列表")
    @GetMapping("/selectList")
    public Result<IPage<Need>> selectList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit,Need car){
        String s = AuthContextUtil.get();
        LambdaQueryWrapper<NeedCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NeedCollect::getAppletUserId,Integer.valueOf(s));
        List<NeedCollect> list = needCollectService.list(wrapper);

//        LambdaQueryWrapper<Need> wrapper1 = new LambdaQueryWrapper<>();
        List<Need> carList = new ArrayList<>();
        if (list.size()>1) {
            for (int i = 0; i < list.size(); i++) {
                Integer carId = list.get(i).getNeedId();
                Need byId = needService.getById(carId);
                carList.add(byId);
            }
        }
//        wrapper1.orderByDesc(Need::getCreateTime);
//        List<Need> list1 = needService.list(wrapper1);
//
//        PageHelper.startPage(page,limit);
//        PageInfo pageInfo = new PageInfo(list1);
//
//        return Result.ok(pageInfo);

        for (Need ipa : carList) {
            LambdaQueryWrapper<Need> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.orderByDesc(Need::getCreateTime).eq(Need::getId,ipa.getId());
            List<Need> list1 = needService.list(wrapper1);
            Page<Need> recommendPage = new Page<>(page,limit);
            IPage<Need> iPage = needService.page(recommendPage,wrapper1);

            for (Need record : iPage.getRecords()) {
                LambdaQueryWrapper<Upvote> wrapper3 = new LambdaQueryWrapper<>();
                wrapper3.eq(Upvote::getAppletUserId,Integer.valueOf(s)).eq(Upvote::getCommentid,2).eq(Upvote::getDynamicid,record.getId());
                Upvote one = upvoteService.getOne(wrapper3);

                if (one!=null){
                    record.setLike(1);
                }else {
                    record.setLike(0);
                }

                LambdaQueryWrapper<NeedCollect> wrapper2 = new LambdaQueryWrapper<>();
                wrapper2.eq(NeedCollect::getAppletUserId,Integer.valueOf(s)).eq(NeedCollect::getNeedId,record.getId());
                NeedCollect one1 = needCollectService.getOne(wrapper2);

                if (one1!=null){
                    record.setCollect(1);
                }else {
                    record.setCollect(0);
                }
            }

            return Result.ok(iPage);
        }

        Page<Need> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<Need> wrapper1 = new LambdaQueryWrapper<>();

        Integer id = car.getId();

        if (id!=null){
            wrapper1.orderByDesc(Need::getCreateTime).eq(Need::getId,id);
        }

        IPage<Need> iPage = needService.page(recommendPage,wrapper1);

        return Result.ok(iPage);

    }

//    @ApiOperation(value = "程序的需求专区收藏详情")
//    @GetMapping("/selectNeed/{id}")
//    public Result selectNeed(@PathVariable Integer id){
//        Need byId = needService.getById(id);
//        byId.setCollect(1);
//        return Result.ok(byId);
//    }

}

